//
//  NZKNetworkErrorView.m
//  ProjectBoss
//
//  Created by 聂志康 on 2018/8/24.
//  Copyright © 2018 weipainetwork. All rights reserved.
//

#import "NZKNetworkErrorView.h"

#define ReloadText @"重新加载"

@interface NZKNetworkErrorView ()
@property (nonatomic, strong) UIImageView *networkErrorImageView;
@property (nonatomic, strong) UILabel *networkErrorTipLabel;
@property (nonatomic, strong) UIButton *networkErrorButton;

@property (nonatomic, copy) NZKNetworkErrorViewRefreshBlock refreshBlock;
@end

@implementation NZKNetworkErrorView

- (instancetype)initWithFrame:(CGRect)frame withRefreshBlock:(NZKNetworkErrorViewRefreshBlock)refreshBlock {
    self = [super initWithFrame:frame];
    if (self) {
        _refreshBlock = refreshBlock;
        self.backgroundColor = hexColor(ffffff);
        [self addSubview:self.networkErrorImageView];
        [self addSubview:self.networkErrorTipLabel];
        [self addSubview:self.networkErrorButton];
    }
    return self;
}

- (void)setTipText:(NSString *)tipText {
    _tipText = tipText;
    self.networkErrorTipLabel.text = tipText;
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    WS
    UIImage *image = self.networkErrorImageView.image;
    CGSize imageSize = CGSizeZero;
    if (image) {
        imageSize = image.size;
    }
    [self.networkErrorImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.top.equalTo(weakSelf.mas_top).with.offset(kHeight(56));
        make.size.mas_equalTo(imageSize);
    }];
    [self.networkErrorTipLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).with.offset(12);
        make.right.equalTo(weakSelf.mas_right).with.offset(-12);
        make.top.equalTo(weakSelf.networkErrorImageView.mas_bottom).with.offset(40);
    }];
    CGRect TitleRect = [ReloadText boundingRectWithSize:CGSizeMake(ScreenWidth, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont fontWithName:@"SourceHanSansCN-Regular" size:12]} context:nil];
    [self.networkErrorButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.networkErrorTipLabel.mas_bottom).with.offset(25);
        make.centerX.equalTo(weakSelf.networkErrorTipLabel.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(TitleRect.size.width+25, TitleRect.size.height+12));
    }];
}

- (UIImageView *)networkErrorImageView {
    if (!_networkErrorImageView) {
        _networkErrorImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"networkErrorImage"]];
    }
    return _networkErrorImageView;
}

- (UILabel *)networkErrorTipLabel {
    if (!_networkErrorTipLabel) {
        _networkErrorTipLabel = [[UILabel alloc] init];
        _networkErrorTipLabel.textColor = hexColor(999999);
        _networkErrorTipLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:12];
        _networkErrorTipLabel.textAlignment = NSTextAlignmentCenter;
        _networkErrorTipLabel.text = @"阿欧~网络好像出错了";
        _networkErrorTipLabel.numberOfLines = 0;
    }
    return _networkErrorTipLabel;
}

- (UIButton *)networkErrorButton {
    if (!_networkErrorButton) {
        _networkErrorButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth/2-40, 245, 80, 24)];
        [_networkErrorButton setTitleColor:NAV_COLOR forState:UIControlStateNormal];
        _networkErrorButton.layer.cornerRadius = 5;
        _networkErrorButton.layer.masksToBounds = true;
        _networkErrorButton.layer.borderColor = NAV_COLOR.CGColor;
        _networkErrorButton.layer.borderWidth = 0.5;
        [_networkErrorButton setTitle:ReloadText forState:UIControlStateNormal];
        _networkErrorButton.titleLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:12];
        [_networkErrorButton addTarget:self action:@selector(networkErrorButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _networkErrorButton;
}

- (void)networkErrorButtonClick {
    if (_refreshBlock) {
        _refreshBlock();
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
